local IndexController = Class('controllers.index', LoadApplication('controllers.base'))
local user_service = LoadApplication('models.service.user')
local func = LoadLibrary('function')
local mysql= LoadLibrary('mysql')
local redis= LoadLibrary('redis')

function IndexController:__construct()
	self.parent:__construct()
	--dump(self.parent.get.VA_POST)
	--dump(self.parent.get.VA_GET)
	--print_r(self.parent.get.d)
	--func.dump({1,2,3});
end

function IndexController:index()

	--func.dump(ngx.today());
	--func.dump(ngx.time());
	--func.dump(ngx.utctime());
	--func.dump(ngx.localtime());

	--func.dump(func.encrypt('hello'));
	--func.dump(func.decrypt('br8xwt0ARkWc8semS8FS+TjpaGt2KLbjm59ofD70iUfvGllboV61vjkNbSjU46Q18VP7ruNF72iByc8YZ4turTmaxtLp59Z5yym/wrZ7DYZ2zkIRpsMCBo+uSJgNNvZmneVCKL9b4fzVvvrt8L4WstLyZ0ilRoFivC5rWPRn0Dk='));

	--func.dump(func.sign('world'));
	--func.dump(func.verify('world','ZLAo4O6PxD4gWTMJAOEWy8MfdOPjIcWj2w1cYgjbYO6ph2EB+/gN8a24jTl/oPWMcCYTETDqJInd5vqAqUmD8sRwZp7rifTo0bFk9euNGV/eospTX6tZVGW8ZlqJcW2KSOY4YnyOYZ1RHnV5gRemizmZN0LUa1DJX5IF17khGSk='));
	local t=func.rand(111,999);
	--	func.dump(t);
	--	func.dump(func.rand(1, 1000));

    return json(t);
end

function IndexController:uid1()
  local rd = redis:new()
  local key="go_clock";
  local res, err = rd:incr(key)
  math.randomseed(res)
  local code = ngx.time() .. res .. math.random(10000, 99999);
  code= func.substr(code, -11);
  return code;
end

function IndexController:uid2()
	local cache = ngx.shared.cache
    local key = "go_clock"
	local res = cache:get(key)
	if res then
		res = cache:incr(key, 1)
	else
		res = cache:set(key, 1)
	end
	math.randomseed(res)
	local code = ngx.time() .. res .. math.random(10000, 99999);
	code= func.substr(code, -11);
	return code;
end


function IndexController:mysql()
	--local db = mysql:new()
	local sql = "select * from go_carbrand Order by letter ASC limit 5"
	local res, err, errno, sqlstate = mysql:query(sql)
	if not res then
		ngx.say("sql error:"..errno)
		return {}
	end

	return json(res)
end

function IndexController:redis()
	local rd = redis:new()
	local key="go_carmodel274";

	local res, err = rd:get(key)
	if not res then
		res=1000;
		local ret, err = rd:setex(key,  60, res);
		if not ret then
			ngx.say("setEx redis key error:"..err)
		end
	end

	return html(res)
end

function IndexController:base64()
	local src = '123456'
	local dst = codec.base64_encode(src)
	ngx.say(dst);

	local dsrc = codec.base64_decode(dst)
	print(dsrc)
	ngx.say(dsrc);
	return dsrc;
end

function IndexController:rsa()
	local codec = require('codec')
	local src = '123456'
	local pubpem = [[-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsxjKD2lnmkmELoo5QphM/VdRE
JKym26R0T+19JDa3MVZFDbwgUGT8XM8bElrKgxexhTVRt07btyIejdbiPx7sCbWc
VP8peZI+QZEVVzaE2Ci5n0lP9v9GUSl0QfZU94uIwl++BVq0VFvbHax/R/q4oTRD
1u73ASM27QW42+cJFwIDAQAB
-----END PUBLIC KEY-----]]
	ngx.say(pubpem);
	local typ = 2
	local bs = codec.rsa_public_encrypt(src, pubpem, typ)
	ngx.say(bs);
	local dst = codec.base64_encode(bs)
	ngx.say(dst);
	--print(dst)

	local privpem = [[-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCsxjKD2lnmkmELoo5QphM/VdREJKym26R0T+19JDa3MVZFDbwg
UGT8XM8bElrKgxexhTVRt07btyIejdbiPx7sCbWcVP8peZI+QZEVVzaE2Ci5n0lP
9v9GUSl0QfZU94uIwl++BVq0VFvbHax/R/q4oTRD1u73ASM27QW42+cJFwIDAQAB
AoGALHoNMQI52HBgSSV8q2hFVi2bKjuisoWibUrSIT/8UeaChd5GSq9Hf+vIaPit
pKpgpBNdqX6d71PSlbj/01hadg5IxrGWQZWzT/3IzuhTxAu4TkztUJelGRcM6ykZ
5AxijiIxTLWSY/ygtEaM2QShhl8dCReNT+oIDGf/iMSTVykCQQDl07WZR9ATReVc
vM7/v9iiz/g1Tj9/8AOuyYOZ5kp5a8IAr48dXixzuTZY66RwPj/J5vrzLuHc7Uc0
RAi4hgmTAkEAwHMxP0KVOzDH49SsiUjfOycqrBl68QCXUWQj2mi7Bb1pLryoYDFv
FTuk6pxKyfr5O8M2s8thTz6f3EO7hFqk7QJAdX8Ly2ZkYUYNoaDBbwzEk1AhhBcR
7bVmHJjXV/ndP0Aw+arHTutTbIJW35TxB5U7hVw6FdN1Ez6XdYgGsVeNUwJAEjlW
SoVFmGtQInT7Oaza5sEYu19WUwgZTC3Nb1tHio2bLj/TOfi0ajBRt53BP0sy2sPr
pC74MgbeIH+RfEERKQJBAIpPkQztkbpZwD9gDiK86U+HHYZrhglxgfDIXYwTH3z/
KCrfyNxiH2im9ZhwuhLs7LDD7wDPHUC5BItx2tYN10s=
-----END RSA PRIVATE KEY-----]]
	local dbs = codec.base64_decode(dst)
	local dsrc = codec.rsa_private_decrypt(dbs, privpem)
	--print(dsrc)
	ngx.say(dsrc);

	return dsrc;
end

-- curl http://localhost:9110/get?ok=yes
function IndexController:get()
    --local get = self:getRequest():getParams()
    --print_r(get)
    do return 'get' end
end

-- curl -X POST http://localhost:9110/post -d '{"ok"="yes"}'
function IndexController:post()
    do return json(self.parent.get) end
end

-- curl -H 'accept: application/vnd.YOUR_APP_NAME.v1.json' http://localhost:9110/api?ok=yes
function IndexController:api_get()
    local api_get = self:getRequest():getParams()
    ngx.say(jstr)
    do return 'api' end
end

return IndexController
